<span style="color: blue;"><span style="font-size: 16px;"> **親愛的,咱不用new就來通殺這些...物件。
String, Number, Boolean, Array, JSON, RegExp** </span></span>
相對於使用new的建構式,實字是更簡單的選擇:
(1)陣列實字:用逗號分隔的一串值,並用中括號包起來
(2)JSON:由物件實字和陣列實字組成,用以交換的資料格式
(3)正規表示式實字
(4)除了Date()可以之外,其它應避免的內建建構式:String(), Number(), Boolean()
建議使用全螢幕高畫質觀看,之後再上JSFiddle褻玩一番。
var result = "結果",
str = new String("總裁 海綿寶寶"),
str1 = "總裁 海綿寶寶 鐵大";
num = new Number(12345.12345),
num1 = 12345.12345,
b = new Boolean(false),
bb = true,
a = new Array("外獅佬","超人","大神");
aa = ["外獅佬","超人","大神", "J大"],
obj = {name: "海綿寶寶", address: "比奇堡"},
s = JSON.stringify(obj),
o = JSON.parse(s),
r = new RegExp("[a-z]", "gim"),
rr = /[a-z]/gim;
result = str1.length;
result = num1.toPrecision(12);
result = bb.toString();
result = aa.length;
result = obj.name;
result = s;
result = o.address;
result = "This is a book.".replace(rr, "Z");
document.getElementById("demo").innerHTML = result;